<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>短信平台</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="robots" content="all,follow">
    <!-- Bootstrap CSS-->
    <link rel="stylesheet" th:href="@{/vendor/bootstrap/css/bootstrap.min.css}">
    <!-- Font Awesome CSS-->
    <link rel="stylesheet"  th:href="@{/vendor/font-awesome/css/font-awesome.css}">
    <!-- Fontastic Custom icon font-->
    <link rel="stylesheet" th:href="@{/css/fontastic.css}">
    <!-- Google fonts - Roboto -->
    <!--<link rel="stylesheet" th:href="@{https://fonts.googleapis.com/css?family=Roboto:300,400,500,700}">-->
    <!-- jQuery Circle-->
    <link rel="stylesheet" th:href="@{/css/grasp_mobile_progress_circle-1.0.0.min.css}">
    <!-- Custom Scrollbar-->
    <link rel="stylesheet" th:href="@{/vendor/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.css}">
    <!-- theme stylesheet-->
    <link rel="stylesheet" th:href="@{/css/style.blue.css}" id="theme-stylesheet">
    <!-- Custom stylesheet - for your changes-->
    <link rel="stylesheet" th:href="@{/css/custom.css}">
    <!--<link rel="stylesheet" th:href="@{/css/zTree/demo.css}" type="text/css">-->
    <link rel="stylesheet" th:href="@{/css/zTree/zTreeStyle/bootstrapStyle.css}" type="text/css">
    <!-- Favicon-->
    <link rel="shortcut icon" th:href="@{/img/favicon.ico}">
    <link rel="stylesheet" th:href="@{/css/toastr.css}">
    <!-- Tweaks for older IEs--><!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->
</head>
<style>
    input.error { border: 1px solid red; }
    label.error {
        font-weight: bold;
        color: red;
    }
    .ztree li ul.line{ height: 80%; }
</style>
<!-- JavaScript files-->
<script type="text/javascript" th:src="@{/js/jquery.js}" ></script>
<script type="text/javascript" th:src="@{/vendor/popper.js/umd/popper.min.js}"> </script>
<script th:src="@{/vendor/bootstrap/js/bootstrap.min.js}"></script>
<script th:src="@{/js/grasp_mobile_progress_circle-1.0.0.min.js}"></script>
<script th:src="@{/vendor/jquery.cookie/jquery.cookie.js}"> </script>
<script th:src="@{/vendor/chart.js/Chart.min.js}"></script>
<script th:src="@{/vendor/jquery-validation/jquery.validate.min.js}"></script>
<script th:src="@{/js/jquery-validation/jquery.validate.min.js}"></script>
<script th:src="@{/vendor/malihu-custom-scrollbar-plugin/jquery.mCustomScrollbar.concat.min.js}"></script>
<script th:src="@{/js/bootstrapValidator.min.js}"></script>
<script type="text/javascript" th:src="@{/js/zTree/jquery.ztree.core.js}"></script>
<script type="text/javascript" th:src="@{/js/zTree/jquery.ztree.excheck.js}"></script>
<script type="text/javascript" th:src="@{/js/zTree/jquery.ztree.exedit.js}"></script>
<!-- Main File-->
<script th:src="@{/js/toastr.min.js}"></script>
<script th:src="@{/js/front.js}"></script>
<script type="text/javascript" th:src="@{/js/base.js}"></script>
<meta name="_csrf" th:content="${_csrf.token}"/>
<meta name="_csrf_header" th:content="${_csrf.headerName}"/>
<body>
<!-- Side Navbar -->
<div th:replace="~{/pages/common/side_navbar.html}"></div>

<div class="page">
    <!-- navbar-->
    <div th:replace="~{/pages/common/header_navbar.html}"></div>
    <!-- Breadcrumb-->
    <div class="breadcrumb-holder">
        <div class="container-fluid">
            <ul class="breadcrumb">
                <li class="breadcrumb-item"><a href="#">角色管理</a></li>
                <li class="breadcrumb-item active">角色信息</li>
            </ul>
        </div>
    </div>
    <!--新增用户div start -->
    <section class="forms">
        <div class="container-fluid">
            <div class="col-lg-12 mt-3">
                <div class="card">
                    <div class="card-header d-flex align-items-center" th:if="${null == role.id}">
                        <h4>新增</h4>
                    </div>
                    <div class="card-header d-flex align-items-center" th:if="${null != role.id}">
                        <h4>编辑</h4>
                    </div>

                    <!--数据字段-->
                    <div class="card-body">
                        <form class="form-horizontal" th:action="@{/sys/role/save}" method="post">
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">角色名称  <em style="color: red;">*</em></label>
                                <div class="col-sm-4">
                                    <input type="hidden" th:value="${role.id}" name="roleId" class="roleId"/>
                                    <input type="text" placeholder="" th:value="${role.roleDesc}"
                                           name="roleDesc" id="roleDesc" class="form-control"  readonly />
                                </div>
                            </div>
                            <div class="line"></div>
                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">角色代码  </label>
                                <div class="col-sm-4">
                                    <input type="text" placeholder="必须以ROLE_开头" name="roleName" id="roleName"
                                           th:value="${role.roleName}"  class="form-control"  readonly />
                                    <input th:value="${menuList}" id="menuList" type="hidden"/>
                                </div>
                            </div>
                            <div class="line"></div>

                            <div class="form-group row">
                                <label class="col-sm-2 form-control-label">权限 </label>
                                <div class="col-sm-4">
                                    <div class="zTreeDemoBackground fl m400 mt20">
                                        <ul id="treeDemo" class="ztree"></ul>
                                    </div>
                                </div>
                            </div>
                            <div class="line"></div>
                            <div class="form-group row">
                                <div class="col-sm-4 offset-sm-2">
                                    <a th:href="@{/sys/role/list}" class="btn btn-secondary ml-1">返回</a>
                                    <a  href="#" class="btn btn-success ml-5 saveMeunRole">保存</a>
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>

        </div>
    </section>
    <!--新增用户div end -->
</div>

</body>
<script type="text/javascript">
    $(function () {
        //ztree 方法
        var setting = {
            check: {
                enable: true
            },
            data: {
                simpleData: {
                    enable: true
                }
            },
            view: {
              showLine:false
            },
            chkboxType: { "Y" : "ps", "N" : "ps" }
        };
        var nodesStr = $("#menuList").val();
        var zNodes =JSON.parse(nodesStr);
        var code;
        function setCheck() {
            var zTree = $.fn.zTree.getZTreeObj("treeDemo"),
                py = $("#py").attr("checked")? "p":"",
                sy = $("#sy").attr("checked")? "s":"",
                pn = $("#pn").attr("checked")? "p":"",
                sn = $("#sn").attr("checked")? "s":"",
                type = { "Y":'ps', "N":'ps'};
            zTree.setting.check.chkboxType = type;
            showCode('setting.check.chkboxType = { "Y" : "' + type.Y + '", "N" : "' + type.N + '" };');
        }
        function showCode(str) {
            if (!code) code = $("#code");
            code.empty();
            code.append("<li>"+str+"</li>");
        }

        $(document).ready(function(){
            let treeObj  = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
            //treeObj.expandAll(false)
            setCheck();
            $("#py").bind("change", setCheck);
            $("#sy").bind("change", setCheck);
            $("#pn").bind("change", setCheck);
            $("#sn").bind("change", setCheck);

        });
        let header = $("meta[name='_csrf_header']").attr("content");
        let token =$("meta[name='_csrf']").attr("content");



        //保存方法
        $(".saveMeunRole").click(function () {
            var treeObj=$.fn.zTree.getZTreeObj("treeDemo"),
                nodes=treeObj.getCheckedNodes(true);
            var ids ="";
            for(var i=0;i<nodes.length;i++){
                ids+=nodes[i].id + ",";
            }
            var  roleId = $(".roleId").val();
           console.log(roleId)
            $.ajax({
                //请求方式
                type: "GET",
                //请求地址
                url: $appName+"/api/role/saveMenuRole?menuIds="+ids+"&roleId="+roleId,
                beforeSend : function(xhr) {
                    xhr.setRequestHeader(header, token);
                },
                success: function (result) {
                    if (result.success) {
                        toastr.success(result.msg);
                        setTimeout(function () {
                                window.location.href= $appName+"/sys/role/list";
                            },
                            2000
                        )
                    } else {
                        toastr.success(result.msg);
                    }
                }
            })
        })
    })


</script>

</html>